package kines.ct;

import kines.SerializableDB;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.sql.*;
import java.util.ArrayList;
import kines.Utils;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class Goals implements SerializableDB {
  /**
   * Sales Goals Type
   */
  public static int SALES = 0;
  /**
   * Prospection Goals Type
   */
  public static int PROSPECTION = 1;
  /**
   * Sales SOLD Terrain Goal Type
   */
  public static int SOLD = 0;
  /**
   * Sales Reserved Terrain Goal Type
   */
  public static int RESERVED = 1;
  /**
   * Sales Square Metes Goal Type
   */
  public static int MTS2 = 2;
  /**
   * Sales Money Goal Type
   */
  public static int MONEY = 3;
  /**
   * Prospection Personal Prospect Goal Type
   */
  public static int PERSONAL = 0;
  /**
   * Prospection Referred Prospect Goal Type
   */
  public static int REFERRED = 1;
  /**
   * Prospection Publicity Prospect Goal Type
   */
  public static int PUBLICITY = 2;
  /**
   * Goal Quantity
   */
  public static int GOAL = 0;
  /**
   * Real Quantity
   */
  public static int REAL = 1;
  /**
   * Effectiveness porcentage beetwen REAL AND GOAL
   */
  public static int DIF = 2;
  /**
   * Identifier
   */
  private int id ;
  protected void setId( int id ){
    this.id = id;
  }
  public int getId( ){
    return id;
  }
  /**
   * Start Date Goals
   */
  private Calendar startDate;
  public Calendar getStartDate( ){
    return startDate;
  }
  public void setStartDate( int year, int month, int day ){
    if( startDate == null ){
      startDate = Calendar.getInstance();
    }
    startDate.set(year,month,day);
  }
  /**
   * End Date Goals
   */
  private Calendar endDate;
  public Calendar getEndDate( ){
    return endDate;
  }
  public void setEndDate( int year, int month, int day ){
    if( endDate == null ){
      endDate = Calendar.getInstance();
    }
    endDate.set(year,month,day);
  }
  /**
   * Sales Person to those Goals
   */
  private String salesPersonCode;
  public String getSalesPersonCode( ){
    return salesPersonCode;
  }
  public void setSalesPersonCode( String salesPersonCode ){
    this.salesPersonCode = salesPersonCode;
  }
  /**
   * Development Code to those Goals
   */
  private String developmentCode;
  public String getDevelopmentCode( ){
    return developmentCode;
  }
  public void setDevelopmentCode( String developmentCode ){
    this.developmentCode = developmentCode;
  }
  /**
   * Gets description of this set of goals ( startDate---endDate )
   * @return
   */
  public String getDescription( ){
    String desc = "";
    Utils utils = Utils.getInstance();
    if( endDate != null && startDate != null ){
      String strEndDate = endDate.get(Calendar.DATE)+"/"+utils.getMonthSpanish(endDate.get(Calendar.MONTH))+"/"+endDate.get(Calendar.YEAR);
      String strStartDate = startDate.get(Calendar.DATE)+"/"+utils.getMonthSpanish(startDate.get(Calendar.MONTH))+"/"+startDate.get(Calendar.YEAR);
      desc = strStartDate+"---"+strEndDate;
    }
    return desc;
  }
  /**
   * Goals
   */
  private double goals[][][];
  /**
   * Gets Especific Goal or Real Quantity
   * @param type Goal.SALES or GOAL.PROSPECTION
   * @param especificType if type is SALES you could use Goal.SOLD, Goal.RESERVED, Goal.MTS2 and Goal.MONEY, if type is PROSPECTION you could use Goal.PERSONAL, Goal.REFERRED AND Goal.PUBLICITY
   * @param goalType REAL, GOAL or DIF ( TO DIF TYPE RESULT IS IN PORCENT )
   * @return
   */
  public double getGoal( int type, int especificType , int goalType ){
    if( goalType == Goals.DIF ){
      if( goals[type][Goals.GOAL][especificType] == 0 )return 0;
      else return (goals[type][Goals.REAL][especificType] /
              goals[type][Goals.GOAL][especificType]) * 100;
    }
    return goals[type][goalType][especificType];
  }
  /**
   * Sets Especific Goal
   * @param type Goal.SALES or GOAL.PROSPECTION
   * @param especificType if type is SALES you could use Goal.SOLD, Goal.RESERVED, Goal.MTS2 and Goal.MONEY, if type is PROSPECTION you could use Goal.PERSONAL, Goal.REFERRED AND Goal.PUBLICITY
   * @param goalType REAL, GOAL or DIF ( TO DIF TYPE RESULT IS IN PORCENT )
   * @param value
   */
  public void setGoal( int type, int especificType , double value ){
    goals[type][Goals.GOAL][especificType] = value;
  }
  /**
   * Sets Especific Real
   * @param type Goal.SALES or GOAL.PROSPECTION
   * @param especificType if type is SALES you could use Goal.SOLD, Goal.RESERVED, Goal.MTS2 and Goal.MONEY, if type is PROSPECTION you could use Goal.PERSONAL, Goal.REFERRED AND Goal.PUBLICITY
   * @param goalType REAL, GOAL or DIF ( TO DIF TYPE RESULT IS IN PORCENT )
   * @param value
   */
  protected void setReal( int type, int especificType , double value ){
    goals[type][Goals.REAL][especificType] = value;
  }
  /**
   * Default Constructor
   */
  public Goals( ){
    int types;
    goals = new double[2][2][];
    goals[0][0] = new double[4];
    goals[0][1] = new double[4];
    goals[1][0] = new double[3];
    goals[1][1] = new double[3];

  }
  /**
   * Fill Goals object from Db
   * @param rs_get_goals
   * @param type
   * @return
   * @throws java.lang.Exception
   */
  private static Goals getGoals( ResultSet rs_get_goals , int type )throws Exception{
    Goals goals = null;
    if( rs_get_goals != null ){
      goals = new Goals( );
      goals.setSalesPersonCode(rs_get_goals.getString("CVEVENDEDOR"));
      goals.setDevelopmentCode(rs_get_goals.getString("CVEDESARROLLO"));
      if( type == Goals.SALES ){
        goals.setGoal(Goals.SALES, Goals.SOLD,rs_get_goals.getDouble("VENDIDOS_OBJETIVO"));
        goals.setGoal(Goals.SALES, Goals.MONEY,rs_get_goals.getDouble("VENTA_OBJETIVO"));
        goals.setGoal(Goals.SALES, Goals.MTS2,rs_get_goals.getDouble("METROS_OBJETIVO"));
        goals.setGoal(Goals.SALES, Goals.RESERVED,rs_get_goals.getDouble("APARTADOS_OBJETIVO"));
        goals.setReal(Goals.SALES, Goals.SOLD, rs_get_goals.getDouble("VENDIDOS_REAL"));
        goals.setReal(Goals.SALES, Goals.MONEY, rs_get_goals.getDouble("VENTA_REAL"));
        goals.setReal(Goals.SALES, Goals.MTS2, rs_get_goals.getDouble("METROS_REAL"));
        goals.setReal(Goals.SALES, Goals.RESERVED, rs_get_goals.getDouble("APARTADOS_REAL"));
      }else{
        goals.setGoal(Goals.PROSPECTION, Goals.PERSONAL,rs_get_goals.getDouble("personales_objetivo"));
        goals.setGoal(Goals.PROSPECTION, Goals.PUBLICITY,rs_get_goals.getDouble("publicidad_objetivo"));
        goals.setGoal(Goals.PROSPECTION, Goals.REFERRED,rs_get_goals.getDouble("referidos_objetivo"));
        goals.setReal(Goals.PROSPECTION, Goals.PERSONAL, rs_get_goals.getDouble("personales_real"));
        goals.setReal(Goals.PROSPECTION, Goals.PUBLICITY, rs_get_goals.getDouble("publicidad_real"));
        goals.setReal(Goals.PROSPECTION, Goals.REFERRED, rs_get_goals.getDouble("referidos_real"));
      }
    }
    return goals;
  }
  /**
   * Gets Goals for this sales person and development
   * @param developmentCode Development Code
   * @param year
   * @param type Goals.PROSPECTION or Goals.SALES
   * @return Array of Goals
   */
  public static Object[] getGoalsBySalesPerson(String developmentCode, int year, int type) throws Exception{
    ArrayList goals = new ArrayList( );
    Driver DriverRs = (Driver) Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement stmt_get_goals = ConnRs.createStatement();
    String str_get_goals = "";
    if( type == Goals.PROSPECTION )str_get_goals = "exec REPORTE_OBJETIVO_PROSPECCION_SP_YEAR '"+developmentCode+"', "+year;
    else str_get_goals = "exec REPORTE_OBJETIVO_VENTAS_SP_YEAR '"+developmentCode+"', "+year;
    ResultSet rs_get_goals = stmt_get_goals.executeQuery(str_get_goals);
    while( rs_get_goals.next() ){
      goals.add(Goals.getGoals(rs_get_goals,type));
    }
    ConnRs.close();
    return goals.toArray();
  }
  /**
   * Gets Goals for this sales person and development in this year and this month
   * @param developmentCode
   * @param year
   * @param month
   * @param type Goals.PROSPECTION or Goals.SALES
   * @return Array of Goals
   */
  public static Object[] getGoalsBySalesPerson(String developmentCode, int year, int month, int type) throws Exception{
    ArrayList goals = new ArrayList( );
    Driver DriverRs = (Driver) Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement stmt_get_goals = ConnRs.createStatement();
    String str_get_goals = "";
    if( type == Goals.PROSPECTION )str_get_goals = "exec REPORTE_OBJETIVO_PROSPECCION_SP '"+developmentCode+"', "+year+", "+month+"";
    else str_get_goals = "exec REPORTE_OBJETIVO_VENTAS_SP '"+developmentCode+"', "+year+", "+month+"";
    ResultSet rs_get_goals = stmt_get_goals.executeQuery(str_get_goals);
    while( rs_get_goals.next() ){
      goals.add(Goals.getGoals(rs_get_goals,type));
    }
    ConnRs.close();
    return goals.toArray();
  }
  /**
   * Gets Goals for this sales person and development
   * @param developmentCode
   * @param type Goals.PROSPECTION or Goals.SALES
   * @return Null if this goals doest exist, Goals otherwise
   */
  public static Goals getGoals( int id , int type ) throws Exception{
    Goals goals = null;
    Driver DriverRs = (Driver) Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement stmt_get_goals = ConnRs.createStatement();
    String str_get_goals = "";
    if( type == Goals.PROSPECTION )str_get_goals = "exec REPORTE_OBJETIVO_PROSPECCION_ID "+id;
    else str_get_goals = "exec REPORTE_OBJETIVO_VENTAS_ID "+id;
    ResultSet rs_get_goals = stmt_get_goals.executeQuery(str_get_goals);
    if( rs_get_goals.next() ){
      goals = Goals.getGoals(rs_get_goals,type);
      goals.setId(rs_get_goals.getInt("idregistro"));
      Date dtmp = rs_get_goals.getDate("fechainicio");
      Calendar ctmp = Calendar.getInstance();
      ctmp.setTime(dtmp);
      goals.setEndDate(ctmp.get(Calendar.YEAR),ctmp.get(Calendar.MONTH), ctmp.get(Calendar.DATE));
      dtmp = rs_get_goals.getDate("fechafin");
      ctmp.setTime(dtmp);
      goals.setStartDate(ctmp.get(Calendar.YEAR),ctmp.get(Calendar.MONTH), ctmp.get(Calendar.DATE));
    }
    ConnRs.close();
    return goals;

  }
  /**
   * Gets Completes Goals
   * @param developmentCode
   * @param year
   * @param month
   * @param type
   * @return
   */
  public static Object[] getGoals( String developmentCode, int year, int month, int type )throws Exception{
    ArrayList goals = new ArrayList( );
    Driver DriverRs = (Driver) Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement stmt_get_goals = ConnRs.createStatement();
    String str_get_goals = "";
    if( type == Goals.PROSPECTION )str_get_goals = "exec REPORTE_OBJETIVO_PROSPECCION '"+developmentCode+"', "+year+", "+month+"";
    else str_get_goals = "exec REPORTE_OBJETIVO_VENTAS '"+developmentCode+"', "+year+", "+month+"";
    ResultSet rs_get_goals = stmt_get_goals.executeQuery(str_get_goals);
    while( rs_get_goals.next() ){
      Goals goal = Goals.getGoals(rs_get_goals,type);
      goal.setId(rs_get_goals.getInt("idregistro"));
      Date dtmp = rs_get_goals.getDate("fechainicio");
      Calendar ctmp = Calendar.getInstance();
      ctmp.setTime(dtmp);
      goal.setEndDate(ctmp.get(Calendar.YEAR),ctmp.get(Calendar.MONTH), ctmp.get(Calendar.DATE));
      dtmp = rs_get_goals.getDate("fechafin");
      ctmp.setTime(dtmp);
      goal.setStartDate(ctmp.get(Calendar.YEAR),ctmp.get(Calendar.MONTH), ctmp.get(Calendar.DATE));
      goals.add(goal);
    }
    ConnRs.close();
    return goals.toArray();
  }
  /**
   * Method not implemented
   * @param session
   * @throws java.lang.Exception
   */
  public void saveData(HttpSession session) throws Exception {
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method saveData() not yet implemented.");
  }
  /**
   * Method not implemented
   * @return
   * @throws java.lang.Exception
   */
  public boolean deleteData() throws Exception {
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method deleteData() not yet implemented.");
  }
}